import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
public class Alltime extends MIDlet implements CommandListener{
	Display display = Display.getDisplay(this);
	public Form options;
	public Form editoroptions;
	
	public Form debuginfo;
  Alert al;
  List mainmenu;
  TextBox editor=new TextBox("TextBox Editor :","Welcome to the SandBox",500,TextField.ANY);
  Ticker menuticker=new Ticker("Select anything !!!");
  DateField date=new DateField("Today's : ",DateField.DATE);
  DateField time=new DateField("Time :",DateField.TIME);
  
  Gauge autoload=new Gauge("Auto-Load",true,1,0);
 
  TextField mp3name=new TextField("Music Filename","file:///E:/test.mp3",50,0);
  TextField textfilename=new TextField("Text File Name","file:///E:/text.txt",50,0);
  Command Enter= new Command("Enter",Command.BACK,0);
  Command mainscreen= new Command("Main",Command.SCREEN,1);
  Command Exit= new Command("Exit",Command.STOP,2);
  Command Full=new Command("FullScreen",Command.SCREEN,3);
  Command EditorOptions=new Command("Options",Command.SCREEN,3);
  
  Command ReloadEditor=new Command("Load",Command.SCREEN,4);
  Command SaveEditor=new Command("Save",Command.SCREEN,4);
  Blocks cd;
  Player play;
  game gam;
  public String readFile(String path)
  {InputStream is=null;
  FileConnection fc = null;
  String str="";
  try{
  fc=(FileConnection)Connector.open(path,Connector.READ_WRITE);
  if(fc.exists()){
	  int size=(int)fc.fileSize();
	  is=fc.openInputStream();
	  byte bytes[]=new byte[size];
	  is.read(bytes, 0, size);
	  str=new String(bytes,0,size);
	  }
  }
  catch(Exception e)
  {e.printStackTrace();
  Alert error=new Alert("Error",e.getMessage(),null,AlertType.INFO);
  error.setTimeout(2000);
  display.setCurrent(error);debuginfo.append(e.getMessage());}
  finally
  {
	  try{
		  if(null!=is)is.close();
		  }catch(Exception e){System.out.println(e.getMessage());debuginfo.append(e.getMessage());
		  }}
return str;
  }
  void writeTextFile(String fName, String text)
  {
	  OutputStream os=null;
	  FileConnection fconn=null;
	  try
	  {
		  fconn=(FileConnection)Connector.open(fName, Connector.READ_WRITE);
		  if(!fconn.exists()){
			  fconn.create();
		  }
		  os=fconn.openDataOutputStream();
		  os.write(text.getBytes());
		  fconn.setHidden(false);
		  fconn.setReadable(true);
	  }
	  catch(Exception e)
	  {
		 e.printStackTrace();debuginfo.append(e.getMessage());
		  
	  }
	  finally
	  {
		  try{
			  if(null!=os)
			  {
				  os.close();			  
			  }
			  if(null!=fconn)
			  {
				  fconn.close();			  
			  }
		  }
		  catch(Exception e)
		  {e.printStackTrace();debuginfo.append(e.getMessage());
		  }
		  
	  }
		  
	  }
	  
  
  public Alltime(){
	  
  //TextBox Setup
  editor.addCommand(mainscreen);
  editor.addCommand(EditorOptions);
  editor.addCommand(SaveEditor);
  editor.addCommand(ReloadEditor);
  
  editor.setCommandListener(this);
  editor.setTicker(new Ticker("SmallEditor"));
  if(autoload.getValue()==1)
	  {try{editor.setString(readFile(textfilename.getString()));}
  catch(Exception  e)
  {debuginfo.append(e.getMessage());
  
  e.printStackTrace();
  editor.setString("EmptyFile");
  }}
  
  //List Set-Up
  mainmenu=new List("Main Menu :",3);
  mainmenu.append("Date-Time",null);
  mainmenu.append("Editor",null);
  mainmenu.append("Options",null);
  mainmenu.append("Blocks",null);
  mainmenu.append("Game",null);
  mainmenu.append("Music Creater",null);
  mainmenu.append("Music Player",null);
  mainmenu.append("Errors",null);
  mainmenu.addCommand(Exit);
  mainmenu.addCommand(Enter);
  mainmenu.setCommandListener(this);
  mainmenu.setTicker(menuticker);
  
  //Alert set-up
  al=new Alert("DateAlert");
  al.setType(AlertType.INFO);
  al.setString(new Date().toString());
  al.setTitle("Current Date");
  al.setTimeout(2000);

  //Options Setup
  options=new Form("Options");
  
  editoroptions=new Form("Options");
  options.append("General Options : ");
  options.addCommand(mainscreen);
  options.setCommandListener(this);
  editoroptions.addCommand(Enter);
  editoroptions.addCommand(SaveEditor);
  editoroptions.addCommand(ReloadEditor);
  
  editoroptions.setCommandListener(this);
  
  date.setDate(new java.util.Date());
  time.setDate(new java.util.Date());
  options.append(date);
  options.append(time);
  options.append(mp3name);  
  editoroptions.append(textfilename);
  editoroptions.append(autoload);
  debuginfo=new Form("Errors");
  debuginfo.addCommand(mainscreen);
  debuginfo.setCommandListener(this);
  
      }

public void startApp(){
    
  display.setCurrent(mainmenu);
  }

  public void pauseApp(){
cd=null;options=null;}
  public void destroyApp(boolean unconditional){
  options=null;
  al=null;
  editor=null;
  cd=null;
  notifyDestroyed();}
  public void commandAction(Command c,Displayable d)
  {	  if(c==mainscreen)
		  display.setCurrent(mainmenu);
  else if(c==Enter)
  {
	  switch(mainmenu.getSelectedIndex())
	  {
	  case 0:
		  display.setCurrent(al);
		  break;
	  case 1:
		  display.setCurrent(editor);
		  break;
	  case 2:
		  display.setCurrent(options);
		  break;
	  case 3:		 
cd=new Blocks(debuginfo,display,this);
cd.setCommandListener(cd);
cd.setTitle("Canvas");
		  display.setCurrent(cd);
		  break;
	  case 4:
		  gam=new game();
		  gam.addCommand(mainscreen);
		  gam.setFullScreenMode(true);
		  gam.setCommandListener(this);
		  gam.setTitle("Game");
		  gam.start();
		  display.setCurrent(gam);
		  break;
	  case 5:  
		
		  try{
			 
			Manager.playTone(75, 1000, 200);
			}
		  catch(Exception  e)
		  {e.printStackTrace();debuginfo.append(e.getMessage());
		  }
		  break;
	  case 6:
		  try{
			  play = Manager.createPlayer(mp3name.getString());
			play.start();}
		  catch(Exception  e)
		  {	  
		  debuginfo.append(e.getMessage());
		  
		  e.printStackTrace();
		  		  
		  }
		  break;
	  case 7:
		  display.setCurrent(debuginfo);
	  }
  }
  
  else if(c==Exit){
	writeTextFile(textfilename.getString(),editor.getString());
	  destroyApp(true);
	  notifyDestroyed();
  }
	  

  else if(c==EditorOptions)
  {display.setCurrent(editoroptions);
  }
  else if(c==ReloadEditor)
  {editor.setString(readFile(textfilename.getString()));
	  display.setCurrent(editor);
  }
  else if(c==SaveEditor)
  {writeTextFile(textfilename.getString(),editor.getString());
	  display.setCurrent(editor);
  }
  else cd.repaint();
	  }
  }
